(function($, doc) {
	$.plusReady(function() {
		mui.init({
			pullRefresh: {
				container: '#pullrefresh',
				up: {
					contentrefresh: '',
					callback: pullupRefresh
				}
			}
		});

		//pullupRefresh();
		var user = app.getState();
		var uid = user.user_id;
		var sysCoin = "GATC";
		var currentCoin = 'eth';
		var self = plus.webview.currentWebview();
		var token = self.token;
		//console.log(token);
		var coindata = ethscan.getCoindata(token);
		var coinname = coindata['name'];
		var ctype = coindata['ctype'];
		doc.getElementById('sale_type').innerHTML = '出售' + coinname;
		doc.getElementById('buy_type').innerHTML = '购买' + coinname;
		var myWallet = ethscan.getWallet();
		var pricelist = ethscan.getpriceList();
		var mycoinlist = ethscan.getmycoinlist();
		var optype = 1;
		doc.getElementById('buy_type').addEventListener('tap', function() {
			doc.getElementById('area1').style.display = '';
			doc.getElementById('area2').style.display = 'none';
			doc.getElementById('sale_type').classList.remove('buy-active');
			doc.getElementById('buy_type').classList.add('buy-active');
			optype = 1;

		});
		doc.getElementById('sale_type').addEventListener('tap', function() {
			doc.getElementById('area1').style.display = 'none';
			doc.getElementById('area2').style.display = '';
			doc.getElementById('buy_type').classList.remove('buy-active');
			doc.getElementById('sale_type').classList.add('buy-active');
			optype = 2;
			var cells = document.body.querySelectorAll('#area2 li');
			if(cells.length < 1) {
				setTimeout(function() {
					pullupRefresh();
				}, 300);
			}
		});
		var count = 0;
		var buystart = 0;
		var salestart = 0;

		function pullupRefresh() {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh((count++ > 100));
			var size = 20;

			var url = weburl + 'Home/ChatApi/getTradeList';
			//var uid = 179020;
			var postdata = {
				uid: uid,
				//page : start,
				size: size,
				type: optype,
				btype: token
			};
			if(optype == 1) {
				postdata.page = buystart;
			} else {
				postdata.page = salestart;
			}
			console.log(JSON.stringify(postdata));

			mui.post(url, postdata, function(res) {
				console.log(JSON.stringify(res));
				if(res.data && res.data.length > 0) {
					//start = start + res.data.length;
					if(optype == 1) {
						buystart = buystart + res.data.length;
					} else {
						salestart = salestart + res.data.length;
					}
					document.getElementById('area' + optype).innerHTML = document.getElementById('area' + optype).innerHTML + template("tpl", {
						data: res.data
					});
				} else {
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(true); //参数为true代表没有更多数据了。
				}
			});
		}
		/*
		setTimeout(function() {
			pullupRefresh();
		}, 800);*/
		setTimeout(function() {
			if(mui.os.ios) {
				document.getElementById('pullrefresh').style.marginTop = '50px';
			}
			mui('#pullrefresh').pullRefresh().pullupLoading();
		}, 1000);
		/*
		renderlist();
	
		function renderlist() {
			$.getJSON(weburl + 'Home/ChatApi/getMyTrades?u=' + uid, function(res) {
				var data = res.data;
				if(data.length > 0) {
					doc.getElementById('buyarea').innerHTML = template('datatpl', {
						data: data,
						currentname: data.icon_type,
						syscoinname: sysCoin
					});
				} else {
					doc.getElementById('buyarea').innerHTML = '';
				}

			});
		}
		page.init('buyarea', function() {
			
			if(page.direction == 1) {
				console.log(page.direction);
			}
		});*/
		$("#dataarea").on('tap', "li", function() {
			var xcurrentCoin = this.getAttribute("value");
			$.openWindow({
				url: 'detail.html',
				id: 'marketdetail',
				extras: {
					token: xcurrentCoin,
					dataid: this.id
				},
				createNew: true
			});
		});
	});
}(mui, document));